﻿using CommonFunction;
using MyWPF.StaticData;
using Office;
using System;
using System.Data;
using System.Threading.Tasks;
using System.Windows;
using WPFDevelopers.Controls;

namespace MyWPF.Views
{
    /// <summary>
    /// DeveloperWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeveloperWindow : Window
    {
        public DeveloperWindow()
        {
            InitializeComponent();
        }

        private void 添加一条消息(object sender, RoutedEventArgs e)
        {
            StaticModels.mainWindow.AddNews(DateTime.Now.ToString());

        }

        private void 截屏(object sender, RoutedEventArgs e)
        {
            var screenCut = new ScreenCut();
            screenCut.ShowDialog();

        }

        private void 生成一个word文件到桌面(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns["ID"].ColumnName = "ID";
            dt.Columns.Add("Name", typeof(string));
            dt.Columns["Name"].ColumnName = "Name";
            dt.Columns.Add("Age", typeof(int));
            dt.Columns["Age"].ColumnName = "Age";

            RandomHelper randomHelper1 = new RandomHelper();

            for (int i = 0; i < 100; i++)
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = randomHelper1.GetRandomString(8, false, false, true);
                dr["Name"] = randomHelper1.GetRandomString(10);
                dr["Age"] = randomHelper1.GetRandomString(2, false, false, true);
                dt.Rows.Add(dr);
            }



            RandomHelper randomHelper = new RandomHelper();
            string Str = randomHelper.GetRandomString(5000);
            WordHelper wordHelper = new WordHelper();
            wordHelper.LoadWord();
            wordHelper.AddText(Str, 12, "#3BB15D", 5, 5);
            wordHelper.AddDataTable(dt);

            PCHelper pCHelper = new PCHelper();
            string deskPath = pCHelper.GetDeskPath();
            wordHelper.SaveWord(deskPath, "测试表.docx");
        }


        private async void 刚刚生成的Word转Pdf输出到桌面(object sender, RoutedEventArgs e)
        {
            await Task.Run(()=>
                {
                PCHelper pCHelper = new PCHelper();
                string deskPath = pCHelper.GetDeskPath();

                PdfHelper pdfHelper = new PdfHelper();
                bool result = pdfHelper.WordToPdf(deskPath + "/测试表.docx", deskPath + "/测试表.pdf");
                if (result)
                {
                        System.Windows.MessageBox.Show("转换成功");
                }
                else
                {
                        System.Windows.MessageBox.Show("转换失败");
                }
            });
        }

        private void 添加10条随机日志(object sender, RoutedEventArgs e)
        {
            RandomHelper randomHelper = new RandomHelper();
            
            for (int i = 0; i < 10; i++)
            {
                Task.Delay(100);
                string Str = randomHelper.GetRandomString(20);
                StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                {
                    Context = "无意义",
                    Time = DateTime.Now,
                    Type = Models.Journal.JournalType.日志,
                    User = StaticModels.user.User.UserName
                });
            }
        }

        private void 添加7条当前用户日志(object sender, RoutedEventArgs e)
        {
            RandomHelper randomHelper = new RandomHelper();

            for (int i = 0; i < 7; i++)
            {
                Task.Delay(100);
                string Str = randomHelper.GetRandomString(20);
                StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                {
                    Context = "无意义",
                    Time = DateTime.Now,
                    Type = Models.Journal.JournalType.日志,
                    User = StaticModels.user.User.UserName
                });
            }
        }

        private void 添加2条很长的日志(object sender, RoutedEventArgs e)
        {
            RandomHelper randomHelper = new RandomHelper();

            for (int i = 0; i < 2; i++)
            {
                Task.Delay(100);
                string Str = "";
                for (int j = 0; j < 30; j++)
                {
                    Str += randomHelper.GetRandomString(5) + " ";
                }

                StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                {
                    Context = "无意义",
                    Time = DateTime.Now,
                    Type = Models.Journal.JournalType.日志,
                    User = StaticModels.user.User.UserName
                });
            }
        }

        bool RunCpu;
        private void 开始记录CPU占用(object sender, RoutedEventArgs e)
        {
            RunCpu = true;
            Task.Run(async () =>
            {
                PCHelper pCHelper = new PCHelper();
                string log = "";
                while (RunCpu)
                {
                    float result = pCHelper.GetCpuUsage();
                    log += result.ToString("f2") + "\n";
                    Dispatcher.Invoke(new Action(() =>
                    {
                        Log.Text = log;
                    }));

                }
            });
            
        }

        private void 停止记录CPU占用(object sender, RoutedEventArgs e)
        {
            RunCpu = false;
        }

        private void 获取电脑内存大小(object sender, RoutedEventArgs e)
        {
            PCHelper pCHelper = new PCHelper();
            Log.Text += "电脑使用内存大小" + pCHelper.GetCurrentMemoryUsage() + "G\n" + "电脑内存大小" + pCHelper.GetTotalMemorySize() + "G\n";
        }
    }
}
